// Enter your shader in this window
#define nThread 128


uint nGroupX = 1;
uint nGroupY = 1;
uint nGroupZ = 1;

uint nMergeSize = 2;
uint nMergeSubSize = 1;
bool bUp = true;

Buffer<uint> g_list_UINT1;
RWBuffer<uint> g_list_RW_UINT1;

[numthreads( nThread,  1,  1)] 
void cs_BitonicSort_UINT1(uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex)
{
	uint nElem = (Gid.z*nGroupY*nGroupX + Gid.y*nGroupX + Gid.x)*nThread + GTid.x;
	uint nSwapElem = nElem ^ nMergeSubSize;

	
	if (nSwapElem <= nElem)
		return;
			
	bool bHaveSameBit = (nElem & nMergeSize) == 0;
	
	uint value[2];

	value[0] = g_list_UINT1[nElem];
	value[1] = g_list_UINT1[nSwapElem];
	
	uint result[2] = {value[0], value[1]};
	if (!bHaveSameBit && value[0] > value[1] && bUp)
	{
		result[0] = value[1];
		result[1] = value[0];
	}
	if (bHaveSameBit && value[0] < value[1] && bUp)
	{
		result[0] = value[1];
		result[1] = value[0];
	}
	g_list_RW_UINT1[nElem] = result[0];
	g_list_RW_UINT1[nSwapElem] = result[1];
	
}



technique11 Tech_BitonicSort_UINT1 {
   pass p0 
	{
        SetVertexShader( NULL );
        SetGeometryShader( NULL );
        SetPixelShader( NULL );
		SetComputeShader( CompileShader( cs_5_0, cs_BitonicSort_UINT1()));
    }
}
